<?php
/**
 * Product:     One-Page Checkout Booster
 * Package:     Gold_Gonepage_1.1.1_94708
 * Purchase ID: 9CBm2i1g9YwZZ6QK4IjUneSwxlnJsImwioL1MUWAXd
 * Generated:   2011-07-15 10:07:59
 * File path:   app/code/local/Gold/Gonepage/Controller/Action.php
 * Copyright:   (c) 2011 GOLD, Inc.
 */
class Gold_Gonepage_Controller_Action extends Mage_Checkout_Controller_Action
{
    
    protected function _getOnepage()
    {
        return Mage::getSingleton('checkout/type_onepage');
    }
    
    protected function _getStepHtml($step)
    {
        $layout = Mage::getModel('core/layout'); 
        $update = $layout->getUpdate();
        $update->load('gonepage_checkout_' . $step);
        $layout->generateXml();
        $layout->generateBlocks();
        $output = $layout->getOutput();
        return $output;
    }    
    
    protected function _getMessagesHtml()
    {   
        $quote = $this->_getOnepage()->getQuote();
        if ($quote->hasItems() && !$quote->validateMinimumAmount())
        {
            $error = Mage::getStoreConfig('sales/minimum_order/error_message');
            $this->_getOnepage()->getCheckout()->addError($error);
        }
        
        foreach ($quote->getMessages() as $message) {
            if ($message) {
                $this->_getOnepage()->getCheckout()->addMessage($message);
            }
        } 

        $layout = $this->loadLayout()
            ->_initLayoutMessages('checkout/session')
            ->_initLayoutMessages('catalog/session');  
        return $this->getLayout()->getMessagesBlock()->getGroupedHtml();  
    }
    
    protected function _extractStepOutput($step, $result = array())
    {     
        $output = Mage::getSingleton('gonepage/save_response')->addStepResponse($step, $result)->toArray();
        $data = $this->getRequest()->getPost('reload_steps');
        if ($data)
        {
            $reloadSteps = explode(',', $data);
            foreach ($reloadSteps as $reloadStep)
            {              
                if($reloadStep=='messages')
                {
                    $html = $this->_getMessagesHtml();
                } else{
                    $html = $this->_getStepHtml($reloadStep);
                }
                $output['update_section'][$reloadStep] = array( 'name' => $reloadStep, 'html' => $html );

            } 
        }
        return $output;   
    }

    
}